package service

import (
	"admin100/app/dao"
	"admin100/app/model"
	"admin100/app/model/commonmodel"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/util/gconv"
	"math"
	"time"
)

var Role = roleService{}

type roleService struct{}

// 添加角色
func (m *roleService) Add(roleName string) bool {
	if has := m.HasByName(roleName); has {
		defErr.Panic(define.NameExist)
	}
	if _, err := dao.MyadminRole.Save(model.MyadminRole{
		Name: roleName,
	}); err != nil {
		defErr.Panic(define.DataOptFail)
	}

	return true
}

// 检测是否是重复角色名称
func (m *roleService) HasByName(roleName string) bool {
	return dao.MyadminRole.HasByName(roleName)
}

func (m *roleService) HasById(id int64) bool {
	return dao.MyadminRole.HasById(id)
}

// 角色列表
func (m *roleService) List(page int, numsPerPage int) (pageInfo commonmodel.PageInfo, data []*model.MyadminRole) {

	nums, err := dao.MyadminRole.Count()
	defErr.IfPanic(err, define.DataOptFail)

	pageInfo = commonmodel.PageInfo{
		Total: nums,
		Cur:   page,
		PNum:  int(math.Ceil(float64(nums) / float64(numsPerPage))),
	}

	//
	data, err = dao.MyadminRole.Page(page, numsPerPage).All()
	defErr.IfPanic(err, define.DataOptFail)

	return pageInfo, data
}

// 获取角色关联渠道菜单
func (m *roleService) FetchMenuRelByRoleId(roleId int64) []int {
	r, err := dao.MyadminRoleMenuRel.M.Fields(dao.MyadminRoleMenuRel.Columns.MenuId).Where(g.Map{
		dao.MyadminRoleMenuRel.Columns.RoleId: roleId,
	}).Array()
	defErr.IfPanic(err, define.DataOptFail)

	return gconv.SliceInt(r)
}

// 设置角色关联到的菜单
func (m *roleService) UpdateMenuRel(relIds []string, roleId string) bool {
	if _, err := dao.MyadminRoleMenuRel.Delete(dao.MyadminRoleMenuRel.Columns.RoleId, roleId); err != nil {
		defErr.Panic(define.DataOptFail)
	}

	var nowTime = gtime.New(time.Now())

	var glist gdb.List
	for _, menuId := range relIds {
		glist = append(glist, map[string]interface{}{
			dao.MyadminRoleMenuRel.Columns.RoleId:  roleId,
			dao.MyadminRoleMenuRel.Columns.MenuId:  menuId,
			dao.MyadminRoleMenuRel.Columns.Created: nowTime.String(),
		})
	}

	if _, err := dao.MyadminRoleChannelRel.DB.BatchInsert(dao.MyadminRoleMenuRel.Table, glist); err != nil {
		defErr.Panic(define.DataOptFail)
	}
	return true
}

// 获取角色关联渠道数据
func (m *roleService) FetchChannelRelByRoleId(roleId int64) []int {
	r, err := dao.MyadminRoleChannelRel.M.Fields(dao.MyadminRoleChannelRel.Columns.ChannelId).Where(g.Map{
		dao.MyadminRoleChannelRel.Columns.RoleId: roleId,
	}).Array()
	defErr.IfPanic(err, define.DataOptFail)

	return gconv.SliceInt(r)
}

// 设置角色关联到的渠道
func (m *roleService) UpdateChannelRel(relIds []string, roleId string) bool {
	if _, err := dao.MyadminRoleChannelRel.Delete(dao.MyadminRoleChannelRel.Columns.RoleId, roleId); err != nil {
		defErr.Panic(define.DataOptFail)
	}

	var nowTime = gtime.New(time.Now())

	var glist gdb.List
	for _, menuId := range relIds {
		glist = append(glist, map[string]interface{}{
			dao.MyadminRoleChannelRel.Columns.RoleId:    roleId,
			dao.MyadminRoleChannelRel.Columns.ChannelId: menuId,
			dao.MyadminRoleChannelRel.Columns.Created:   nowTime.String(),
		})
	}

	if _, err := dao.MyadminRoleChannelRel.DB.BatchInsert(dao.MyadminRoleChannelRel.Table, glist); err != nil {
		defErr.Panic(define.DataOptFail)
	}
	return true
}

func (m *roleService) Del(roleId int64) bool {
	if roleId < 1 {
		return true
	}
	if _, err := dao.MyadminRole.M.Delete(dao.MyadminRole.Columns.Id, roleId); err != nil {
		defErr.Panic(define.DataOptFail)
	}
	return true
}
